Page({
  data: {
    bookList: [],
    imgUrl: "",
    keyWord: "",
    searchList: wx.getStorageSync("searchArr") || [],
  },
  // 调用数据接口的方法
  getBooks() {
    // 调用小程序的 API 发起请求
    wx.showLoading({
      title: "正在加载中",
    });
    wx.request({
      url: "https://hmajax.itheima.net/api/books",
      method: "GET",
      data: {
        creator: "zhangsan",
      },
      success: (result) => {
        console.log(result);
        this.setData(
          {
            // 更新数据，渲染页面
            bookList: result.data.data,
          },
          () => {
            wx.showToast({
              title: "视图和数据更新成功！",
              icon: "success",
            });
          }
        );
      },
      fail: (err) => {},
      complete: () => {
        wx.hideLoading();
      },
    });
  },
  showModal() {
    wx.showModal({
      title: "温馨提示",
      content: "您确定退出登录吗?",
      complete: (res) => {
        console.log(res);
        if (res.cancel) {
        }
        if (res.confirm) {
        }
      },
    });
  },
  showActionSheet() {
    wx.showActionSheet({
      itemList: ["A", "B", "C", "D"],
    });
  },
  async setLocalData() {
    const token = "sdf4546462sdffds";
    const person = {
      name: "张三",
      age: 18,
      sex: "男",
    };
    // 同步
    wx.setStorageSync("token", token);
    wx.setStorageSync("person", person);
  },
  async getLocalData() {
    // 同步
    console.log(wx.getStorageSync("token"));
    console.log(wx.getStorageSync("person"));
  },
  delLocalData() {
    // 同步
    wx.removeStorageSync("token");
    wx.removeStorageSync("person");
  },
  clearLocalData() {
    // 同步
    wx.clearStorageSync();
  },

  // 异步
  setLocalDataAsync() {
    wx.setStorage({
      key: "dog",
      data: { name: "旺财", age: 10, sex: "公" },
    });
  },
  getLocalDataAsync() {
    wx.getStorage({
      key: "dog",
      success: (res) => {
        console.log(res.data);
      },
    });
  },
  delLocalDataAsync() {
    wx.removeStorage({ key: "dog" });
  },
  clearLocalDataAsync() {
    wx.clearStorage();
  },
  // uploadImg() {
  //   wx.chooseMedia({
  //     count: 1,
  //     mediaType: ["image"],
  //     success: (res) => {
  //       this.setData({
  //         imgUrl: res.tempFiles[0].tempFilePath,
  //       });
  //     },
  //     fail: (res) => {},
  //     complete: (res) => {},
  //   });
  // },
  // 3.2 写法2
  // uploadImg() {
  //   wx.chooseMedia({
  //     count: 1,
  //     mediaType: ["image"],
  //   }).then((res) => {
  //     this.setData({
  //       imgUrl: res.tempFiles[0].tempFilePath,
  //     });
  //   });
  // },
  async uploadImg() {
    const res = await wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
    });
    this.setData({
      imgUrl: res.tempFiles[0].tempFilePath,
    });
  },

  // 搜索
  doSearch() {
    // 取出内容
    if (!this.data.keyWord.trim()) {
      wx.showToast({
        title: "搜索内容不能为空",
        icon: "none",
      });
      return;
    }
    // 把数据放入数组
    const tempArr = this.data.searchList;
    tempArr.push(this.data.keyWord);
    this.setData(
      {
        searchList: tempArr,
        keyWord: "",
      },
      () => {
        wx.setStorageSync("searchArr", tempArr);
      }
    );
  },
  // 清除搜索数据
  clearSearch() {
    this.setData(
      {
        searchList: [],
      },
      () => {
        wx.removeStorageSync("searchArr");
      }
    );
  },
  clickMe(e) {
    console.log("点我了", e.target.dataset);
  },
  clickTwo(e) {
    console.log("点我了", e.mark.name);
  },
});
